package com.pospi.xstore.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pospi.xstore.dto.Brand;
import com.pospi.xstore.dto.CustomerBeen;
import com.pospi.xstore.dto.GoodsBeen;
import com.pospi.xstore.dto.GoodsDto;
import com.pospi.xstore.dto.PayBeen;
import com.pospi.xstore.dto.StoreBeen;
import com.pospi.xstore.dto.SupBeen;
import com.pospi.xstore.ui.xs.CustBeen;

import java.util.List;

/**
 * 把加入购物车的商品的信息进行存储和解析
 */
public class SaveOrderInfo {

    /**
     * 把添加的商品的信息通过SharedPreferences保存
     *
     * @param context
     * @param dtos
     */
    public static void saveAsJson(Context context, List<GoodsDto> dtos) {
        Gson gson = new Gson();
        String json = gson.toJson(dtos);
        Log.i("json", json);
        SharedPreferences.Editor editor = context.getSharedPreferences("goodsdto_json", Context.MODE_PRIVATE).edit();
        editor.putString("json", json);
        editor.apply();

    }

    /**
     * 把订单的信息转换为List
     *
     * @param str
     * @return
     */
    public static List<GoodsDto> changeToList(String str) {
        Gson gson = new Gson();
        List<GoodsDto> dtos = gson.fromJson(str, new TypeToken<List<GoodsDto>>() {
        }.getType());
        return dtos;
    }

    public static List<GoodsBeen> changegoodsToList(String str) {
        Gson gson = new Gson();
        List<GoodsBeen> dtos = gson.fromJson(str, new TypeToken<List<GoodsBeen>>() {
        }.getType());
        return dtos;
    }
    public static String changetoToJson(List<GoodsBeen> dtos) {
        Gson gson = new Gson();
        String json = gson.toJson(dtos);
        Log.i("json", json);
        return json;
    }

    public static String changeGoodDtoToJson(List<GoodsDto> dtos) {
        Gson gson = new Gson();
        String json = gson.toJson(dtos);
        Log.i("json", json);
        return json;
    }
    public static String changePaytoToJson(List<PayBeen> dtos) {
        Gson gson = new Gson();
        String json = gson.toJson(dtos);
        Log.i("json", json);
        return json;
    }
    public static List<PayBeen> changepay(String str) {
        Gson gson = new Gson();
        List<PayBeen> dtos = gson.fromJson(str, new TypeToken<List<PayBeen>>() {
        }.getType());
        return dtos;
    }
    public static List<Brand> changebrandToList(String str) {
        Gson gson = new Gson();
        List<Brand> dtos = gson.fromJson(str, new TypeToken<List<Brand>>() {
        }.getType());
        return dtos;
    }
    public static List<StoreBeen> changeStoreToList(String str) {
        Gson gson = new Gson();
        List<StoreBeen> dtos = gson.fromJson(str, new TypeToken<List<StoreBeen>>() {
        }.getType());
        return dtos;
    }
    public static List<SupBeen> changeSupToList(String str) {
        Gson gson = new Gson();
        List<SupBeen> dtos = gson.fromJson(str, new TypeToken<List<SupBeen>>() {
        }.getType());
        return dtos;
    }
    public static List<CustomerBeen> changeCusToList(String str) {
        Gson gson = new Gson();
        List<CustomerBeen> dtos = gson.fromJson(str, new TypeToken<List<CustomerBeen>>() {
        }.getType());
        return dtos;
    }
    public static List<CustBeen> changeCustToList(String str) {
        Gson gson = new Gson();
        List<CustBeen> dtos = gson.fromJson(str, new TypeToken<List<CustBeen>>() {
        }.getType());
        return dtos;
    }
}
